package com.hyz.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hyz.pojo.assetLog.AssetLog;
import com.hyz.service.AssetLogService;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @author HGD
 * @date 2022/10/4 14:10
 */
@Component
public class AssetLogListener {
    @Autowired
    private AssetLogService service;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "batchInsertAssetLogs"),
            exchange = @Exchange(value = "batchInsertAssetLogs")
    ))
    public void batchInsertAssetLogs(String message) {
        ArrayList<AssetLog> assetLogs = JSON.parseObject(message, new TypeReference<ArrayList<AssetLog>>() {
        });
        service.saveBatch(assetLogs);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "insertAssetLog"),
            exchange = @Exchange(value = "insertAssetLog")
    ))
    public void insertAssetLog(String message) {
        AssetLog assetLog = JSON.parseObject(message, AssetLog.class);
        service.saveOrUpdate(assetLog);
    }
}
